package com.zzyl.serve.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author itheima
 * @date 2025-03-02
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1. 根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        // 2. 属性拷贝 nursingPlan到 nursingPlanVo中
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        // 3. 根据护理计划id查询关联的所有护理项目计划表的list
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        // 4. 将list赋值给nursingPlanVo
        nursingPlanVo.setProjectPlans(list);
        // 5. 返回nursingPlanVo
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        //1.创建接受dto的实体
        NursingPlan nursingPlan = new NursingPlan();
        //2.使用工具拷贝
        BeanUtils.copyProperties(dto, nursingPlan);
        //3.保存计划
        save(nursingPlan);
        //4.调用mapper保存计划项目关联表
        return nursingProjectPlanMapper.batchSave(dto.getProjectPlans(), nursingPlan.getId());
    }

    /**
     * 修改护理计划
     *
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.创建接受dto的实体,进行属性拷贝
        //TODO: 进行属性拷贝时,必须两个对象的属性名和类型都相同!!!否则会拷贝失败
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //2.判断DTO中是否有项目列表
        if (nursingPlanDto.getProjectPlans() != null) {
            //2.1先删除旧的关联数据
            nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
            //2.2批量保存新的关联数据
            nursingProjectPlanMapper.batchSave(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        }
        //3.修改计划信息,返回数据
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        //1.删除护理计划关联表数据
        nursingProjectPlanMapper.deleteByPlanId(id);
        //2.删除护理计划数据
        return removeById(id) ? 1:0;
    }

    @Override
    public List<NursingPlan> selectAll() {
        List<NursingPlan> nursingPlans = selectAll();
        return nursingPlans;
    }
}